app.controller('BusRefundListCtrl', function ($scope, ajax, $modal, toaster) {

	$scope.payList = [
		{ id: 1, name: "支付宝" },
		{ id: 2, name: "微信" },
		{ id: 3, name: "微信公众号" },
	];

	//退款状态
	$scope.statusList = [
		{ id: 1, name: "退款中" },
		{ id: 2, name: "完成退款" },
		{ id: 3, name: "退款失败" },
	];

	$scope.queryMap = {};
	$scope.timeRange = {};
	$scope.checkQueryMap = function () {
		if ($scope.statusList.selected != undefined) {
			$scope.queryMap.status = $scope.statusList.selected.id;
		} else if ($scope.statusList.selected == undefined) {
			delete $scope.queryMap.status;
		}
		if ($scope.timeRange.startTime && $scope.timeRange.endTime) {
			var starTime = $scope.timeRange.startTime.pattern("yyyy-MM-dd");
			var endTime = $scope.timeRange.endTime.pattern("yyyy-MM-dd");
			if ($scope.queryMap.status && $scope.queryMap.status === 2) {
				$scope.queryMap.refundTime = starTime + "#@" + endTime;
			} else {
				$scope.queryMap.requestTime = starTime + "#@" + endTime;
			}
		} else if ($scope.timeRange.startTime == null || $scope.timeRange.endTime == null) {
			delete $scope.queryMap.requestTime;
			delete $scope.queryMap.refundTime;
		}
		if ($scope.payList.selected != undefined) {
			$scope.queryMap.orderPayment__payment__id = $scope.payList.selected.id;
		} else if ($scope.payList.selected == undefined) {
			delete $scope.queryMap.orderPayment__payment__id;
		}
		return true;
	};

	$scope.summary = [];
	$scope.doSummary = function () {
		ajax.call(new Req.GET({
			params: $scope.queryMap,
			resource: 'refund/3/summary'
		})).then(function (result) {
			if (result.code && result.code == 200) {
				$scope.summary = result.result;
				$scope.orderPaymentTotal = $scope.summary[0]; //订单支付总金额
				$scope.orderRefundToatal = $scope.summary[1]; //退款总金额
				$scope.serviceCharge = $scope.summary[2]; //退款手续费总金额
				$scope.insurancesToatal = $scope.summary[3]; //保险金额
				$scope.batchNo = $scope.summary[4]; //退款批次数
				$scope.count = $scope.summary[5]; //退票总数
			}
		}, function () { });
	};
	$scope.reload = $scope.page.loader('refund/3/{pageIndex}/{pageSize}/page');
	$scope.$on('pageIndex', function (event, p) {
		$scope.reload(p, $scope.queryMap);
		$scope.doSummary();
	});
	$scope.reload(1);
	$scope.doSummary();

	$scope.query = function () {
		$scope.checkQueryMap();
		$scope.reload(1, $scope.queryMap);
		$scope.doSummary();
	};

	//导出Excel
	$scope.exportExcel = function () {
		$scope.checkQueryMap();
		ajax.call(new Req.POST({
			fields: $scope.queryMap,
			resource: 'refund/bs/export'
		})).then(function (result) {
			if (result.code == 200) {
				window.open(result.result);
				toaster.pop('success', '成功', '导出信息成功。');
			} else {
				toaster.pop('error', '错误', '导出信息失败！！');
			}
		}, function () { toaster.pop('error', '错误', '导出信息失败！'); });
	};

	$scope.rerefund = function (refund) {
		$modal.open({
			templateUrl: 'template/confirmModal.html?v=82a28a704b',
			controller: 'ModalCtrl',
			size: 'sm',
			resolve: {
				param: function () {
					return {
						"data": $scope.current_data,
						"title": "发起再退款",
						"msg": "确定对当前退款单发起再退款？"
					};
				}
			}
		}).result.then(function (param) {
			ajax.call(new Req.POST({
				resource: 'refund/{userId}/{refundId}/re'.format({ userId: $scope.loginUser.id, refundId: refund.id })
			})).then(function (result) {
				if (result && result.code == 200) {
					toaster.pop('success', '成功', '成功发起再次退款。');
				} else {
					toaster.pop('error', '错误', result.result);
				}
			}, function () {
				toaster.pop('error', '错误', '连接错误，请求失败！');
			});
		});
	};
});